﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using Character;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

// Token: 0x020003D8 RID: 984
public class UploaderCardFileList : MonoBehaviour
{
	// Token: 0x1700025B RID: 603
	// (get) Token: 0x06001894 RID: 6292 RVA: 0x000A0584 File Offset: 0x0009E784
	public SEX Sex
	{
		get
		{
			return this.sex;
		}
	}

	// Token: 0x06001895 RID: 6293 RVA: 0x000A058C File Offset: 0x0009E78C
	private void Start()
	{
		this.sexToggleF.isOn = true;
		this.sexToggleF.onValueChanged.AddListener(delegate(bool v)
		{
			this.OnToggleSex(SEX.FEMALE, v);
		});
		this.sexToggleM.onValueChanged.AddListener(delegate(bool v)
		{
			this.OnToggleSex(SEX.MALE, v);
		});
		this.Setup(SEX.FEMALE);
	}

	// Token: 0x06001896 RID: 6294 RVA: 0x000A05E4 File Offset: 0x0009E7E4
	private void Clear()
	{
		for (int i = 0; i < this.toggles.Count; i++)
		{
			UnityEngine.Object.Destroy(this.toggles[i].gameObject);
		}
		this.toggles.Clear();
		this.datas.Clear();
		Resources.UnloadUnusedAssets();
	}

	// Token: 0x06001897 RID: 6295 RVA: 0x000A0640 File Offset: 0x0009E840
	public void Setup(SEX sex)
	{
		this.sex = sex;
		this.Clear();
		this.path = this.directory + ((sex != SEX.FEMALE) ? "/Male/" : "/Female/");
		string[] files = Directory.GetFiles(this.path, "*.png", SearchOption.TopDirectoryOnly);
		for (int i = 0; i < files.Length; i++)
		{
			this.datas.Add(new Card_Data(files[i], this.noLoadTex));
		}
		this.previewImage.transform.parent.gameObject.SetActive(false);
		for (int j = 0; j < this.datas.Count; j++)
		{
			Toggle toggle = UnityEngine.Object.Instantiate<Toggle>(this.item_original);
			toggle.gameObject.SetActive(true);
			RectTransform rectTransform = toggle.transform as RectTransform;
			toggle.transform.SetParent(this.contentArea);
			toggle.group = this.group;
			toggle.isOn = false;
			this.toggles.Add(toggle);
		}
		this.select = -1;
		this.sortType = GlobalData.sortChara;
		this.Sort_WithoutSE(this.sortType);
		this.sortUI.value = this.sortType;
		this.sortUI.onValueChanged.AddListener(new UnityAction<int>(this.Sort));
		this.ListPlacement();
	}

	// Token: 0x06001898 RID: 6296 RVA: 0x000A079C File Offset: 0x0009E99C
	private void Update()
	{
		float y = this.maskArea.sizeDelta.y;
		float y2 = this.contentArea.sizeDelta.y;
		float num = y2 - y;
		float value = 1f;
		if (y2 > 0f)
		{
			value = y / y2;
		}
		this.scroll.size = Mathf.Clamp(value, 0.2f, 1f);
		Vector2 anchoredPosition = this.contentArea.anchoredPosition;
		anchoredPosition.y = num * this.scroll.value;
		this.contentArea.anchoredPosition = anchoredPosition;
		int num2 = -1;
		for (int i = 0; i < this.toggles.Count; i++)
		{
			RectTransform rectTransform = this.toggles[i].transform as RectTransform;
			float y3 = rectTransform.anchoredPosition.y;
			float y4 = rectTransform.sizeDelta.y;
			float num3 = -anchoredPosition.y;
			float num4 = num3 - y;
			float num5 = y3;
			float num6 = y3 - y4;
			if (num6 <= num3 && num5 >= num4)
			{
				this.datas[i].show = true;
			}
			else
			{
				this.datas[i].show = false;
			}
			this.datas[i].isSelect = this.toggles[i].isOn;
			this.datas[i].UpdateSprite(this.toggles[i].image);
			if (this.toggles[i].isOn)
			{
				num2 = i;
			}
		}
		if (num2 == -1 && this.select >= this.datas.Count)
		{
			num2 = this.datas.Count - 1;
		}
		if (num2 != this.select)
		{
			SystemSE.Play(SystemSE.SE.CHOICE);
			this.select = num2;
			this.ChangeSelect();
		}
	}

	// Token: 0x06001899 RID: 6297 RVA: 0x000A09A4 File Offset: 0x0009EBA4
	private void ChangeSelect()
	{
		if (this.select != -1 && !this.toggles[this.select].isOn)
		{
			this.toggles[this.select].isOn = true;
		}
		this.previewImage.transform.parent.gameObject.SetActive(this.select != -1);
		if (this.select != -1)
		{
			Texture2D texture2D = this.datas[this.select].Texture(true);
			Vector2 png_Size = UploaderCardFileList.PNG_Size;
			if (texture2D != null)
			{
				png_Size.x = (float)texture2D.width;
				png_Size.y = (float)texture2D.height;
			}
			this.previewImage.sprite = Sprite.Create(texture2D, new Rect(Vector2.zero, png_Size), png_Size * 0.5f, 100f, 0U, SpriteMeshType.FullRect);
			this.previewName.text = this.datas[this.select].charaInfo.name;
		}
	}

	// Token: 0x0600189A RID: 6298 RVA: 0x000A0ABC File Offset: 0x0009ECBC
	public void ScrollEvent(BaseEventData data)
	{
		if (this.yNum == 0)
		{
			return;
		}
		float num = 1f / (float)this.yNum * this.scrollSpeed;
		float num2 = -Input.mouseScrollDelta.y * num;
		this.scroll.value += num2;
	}

	// Token: 0x0600189B RID: 6299 RVA: 0x000A0B10 File Offset: 0x0009ED10
	public void Sort(int type)
	{
		this.sortType = type;
		GlobalData.sortChara = type;
		SystemSE.Play(SystemSE.SE.CHOICE);
		this.Sort_WithoutSE(type);
	}

	// Token: 0x0600189C RID: 6300 RVA: 0x000A0B2C File Offset: 0x0009ED2C
	public void Sort_WithoutSE(int type)
	{
		if (type == 0)
		{
			List<Card_Data> list = this.datas;
			if (UploaderCardFileList.callback0 == null)
			{
				UploaderCardFileList.callback0 = new Comparison<Card_Data>(UploaderCardFileList.Sort_Date);
			}
			list.Sort(UploaderCardFileList.callback0);
		}
		else if (type == 1)
		{
			List<Card_Data> list2 = this.datas;
			if (UploaderCardFileList.callback1 == null)
			{
				UploaderCardFileList.callback1 = new Comparison<Card_Data>(UploaderCardFileList.Sort_Date);
			}
			list2.Sort(UploaderCardFileList.callback1);
			this.datas.Reverse();
		}
		else if (type == 2)
		{
			List<Card_Data> list3 = this.datas;
			if (UploaderCardFileList.callback2 == null)
			{
				UploaderCardFileList.callback2 = new Comparison<Card_Data>(UploaderCardFileList.Sort_Name);
			}
			list3.Sort(UploaderCardFileList.callback2);
		}
		else if (type == 3)
		{
			List<Card_Data> list4 = this.datas;
			if (UploaderCardFileList.callback3 == null)
			{
				UploaderCardFileList.callback3 = new Comparison<Card_Data>(UploaderCardFileList.Sort_Name);
			}
			list4.Sort(UploaderCardFileList.callback3);
			this.datas.Reverse();
		}
		this.ListPlacement();
		this.ChangeSelect();
	}

	// Token: 0x0600189D RID: 6301 RVA: 0x000A0C28 File Offset: 0x0009EE28
	private static int Sort_Name(Card_Data a, Card_Data b)
	{
		return string.Compare(a.charaInfo.name, b.charaInfo.name, false);
	}

	// Token: 0x0600189E RID: 6302 RVA: 0x000A0C48 File Offset: 0x0009EE48
	private static int Sort_Date(Card_Data a, Card_Data b)
	{
		return DateTime.Compare(a.charaInfo.time, b.charaInfo.time);
	}

	// Token: 0x0600189F RID: 6303 RVA: 0x000A0C68 File Offset: 0x0009EE68
	private void ListPlacement()
	{
		float num = this.maskArea.sizeDelta.x / (float)this.h_num;
		float d = num / UploaderCardFileList.PNG_Size.x;
		this.itemSize = UploaderCardFileList.PNG_Size * d;
		this.yNum = this.datas.Count / this.h_num;
		if (this.datas.Count % this.h_num != 0)
		{
			this.yNum++;
		}
		Vector2 sizeDelta = this.contentArea.sizeDelta;
		sizeDelta.y = this.itemSize.y * (float)this.yNum;
		if (sizeDelta.y < this.maskArea.sizeDelta.y)
		{
			sizeDelta.y = this.maskArea.sizeDelta.y;
		}
		this.contentArea.sizeDelta = sizeDelta;
		float num2 = 0f;
		for (int i = 0; i < this.datas.Count; i++)
		{
			float x = this.itemSize.x * (float)(i % this.h_num);
			Toggle toggle = this.toggles[i];
			RectTransform rectTransform = toggle.transform as RectTransform;
			rectTransform.anchoredPosition = new Vector2(x, num2);
			rectTransform.localScale = Vector3.one;
			if (i % this.h_num == this.h_num - 1)
			{
				num2 -= this.itemSize.y;
			}
			Text componentInChildren = this.toggles[i].GetComponentInChildren<Text>();
			componentInChildren.text = this.datas[i].charaInfo.name;
			if (this.datas[i].HasTex)
			{
				this.datas[i].ChangeTex();
			}
		}
	}

	// Token: 0x060018A0 RID: 6304 RVA: 0x000A0E58 File Offset: 0x0009F058
	public void Replacement(bool noSelect)
	{
		for (int i = 0; i < this.datas.Count; i++)
		{
			this.datas[i].Delete();
		}
		this.datas.Clear();
		string[] files = Directory.GetFiles(this.path, "*.png", SearchOption.TopDirectoryOnly);
		for (int j = 0; j < files.Length; j++)
		{
			this.datas.Add(new Card_Data(files[j], this.noLoadTex));
		}
		for (int k = 0; k < this.toggles.Count; k++)
		{
			UnityEngine.Object.Destroy(this.toggles[k].gameObject);
		}
		this.toggles.Clear();
		for (int l = 0; l < this.datas.Count; l++)
		{
			Toggle toggle = UnityEngine.Object.Instantiate<Toggle>(this.item_original);
			toggle.gameObject.SetActive(true);
			RectTransform rectTransform = toggle.transform as RectTransform;
			toggle.transform.SetParent(this.contentArea);
			toggle.group = this.group;
			toggle.isOn = false;
			this.toggles.Add(toggle);
		}
		if (noSelect)
		{
			this.select = -1;
		}
		else if (this.select >= this.datas.Count)
		{
			this.select = this.datas.Count - 1;
		}
		this.Sort_WithoutSE(this.sortType);
		this.ListPlacement();
		this.ChangeSelect();
	}

	// Token: 0x060018A1 RID: 6305 RVA: 0x000A0FE8 File Offset: 0x0009F1E8
	private void OnToggleSex(SEX change, bool flag)
	{
		if (flag && change != this.sex)
		{
			SystemSE.Play(SystemSE.SE.CHOICE);
			this.Setup(change);
		}
	}

	// Token: 0x060018A2 RID: 6306 RVA: 0x000A100C File Offset: 0x0009F20C
	public Card_Data GetSelectedCard()
	{
		Card_Data result = null;
		if (this.select != -1)
		{
			result = this.datas[this.select];
		}
		return result;
	}

	// Token: 0x04001C05 RID: 7173
	public static readonly Vector2 PNG_Size = new Vector2(252f, 352f);

	// Token: 0x04001C06 RID: 7174
	public Scrollbar scroll;

	// Token: 0x04001C07 RID: 7175
	public Toggle item_original;

	// Token: 0x04001C08 RID: 7176
	public RectTransform maskArea;

	// Token: 0x04001C09 RID: 7177
	public RectTransform contentArea;

	// Token: 0x04001C0A RID: 7178
	public int h_num = 3;

	// Token: 0x04001C0B RID: 7179
	public Texture2D noLoadTex;

	// Token: 0x04001C0C RID: 7180
	public int cacheNum = 100;

	// Token: 0x04001C0D RID: 7181
	public float scrollSpeed = 1f;

	// Token: 0x04001C0E RID: 7182
	public ToggleGroup group;

	// Token: 0x04001C0F RID: 7183
	public Image previewImage;

	// Token: 0x04001C10 RID: 7184
	public Text previewName;

	// Token: 0x04001C11 RID: 7185
	private List<Card_Data> datas = new List<Card_Data>();

	// Token: 0x04001C12 RID: 7186
	private List<Toggle> toggles = new List<Toggle>();

	// Token: 0x04001C13 RID: 7187
	private Vector2 itemSize;

	// Token: 0x04001C14 RID: 7188
	private int yNum;

	// Token: 0x04001C15 RID: 7189
	private int select = -1;

	// Token: 0x04001C16 RID: 7190
	[SerializeField]
	private string directory;

	// Token: 0x04001C17 RID: 7191
	private string path;

	// Token: 0x04001C18 RID: 7192
	[SerializeField]
	private Dropdown sortUI;

	// Token: 0x04001C19 RID: 7193
	[SerializeField]
	private Toggle sexToggleF;

	// Token: 0x04001C1A RID: 7194
	[SerializeField]
	private Toggle sexToggleM;

	// Token: 0x04001C1B RID: 7195
	private int sortType;

	// Token: 0x04001C1C RID: 7196
	private SEX sex;

	// Token: 0x04001C1D RID: 7197
	[CompilerGenerated]
	private static Comparison<Card_Data> callback0;

	// Token: 0x04001C1E RID: 7198
	[CompilerGenerated]
	private static Comparison<Card_Data> callback1;

	// Token: 0x04001C1F RID: 7199
	[CompilerGenerated]
	private static Comparison<Card_Data> callback2;

	// Token: 0x04001C20 RID: 7200
	[CompilerGenerated]
	private static Comparison<Card_Data> callback3;
}
